import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import store from "@/store/index.js";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
  {
    path: "/video",
    name: "video",
    component: () => import("../views/VideoView.vue"),
  },
  {
    path: "/type",
    name: "type",
    component: () => import("../views/TypeView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/talk",
    name: "talk",
    component: () => import("../views/TalkView.vue"),
    beforeEnter: (to, from, next) => {
      console.log(to, from);
      let token = store.state.token;
      if (token) {
        next()
      } else {
        next({name:"login",params:{name:"talk"}})
      }
    }
  },
  {
    path: "/mine",
    name: "mine",
    component: () => import("../views/MineView.vue"),
    beforeEnter: (to, from, next) => {
      console.log(to, from);
      let token = store.state.token;
      if (token) {
        next()
      } else {
        next({name:"login",params:{name:"mine"}})
      }
    }
  }
];

const router = new VueRouter({
  routes,
});
// 权限管理

export default router;
